/**
 * Created by gengrong on 16/4/15.
 */
import { Form, Input, Modal } from 'antd';
import * as React from 'react';

class UserRoleAddModalForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      createState: {}
    };
  }

  render() {
    const idProps = this.props.form.getFieldProps('id', {
      rules: [
        { required: true, message: `请填写${this.props.title} ID` }
      ],
      trigger: ['onBlur', 'onChange'],
    });

    return (
      <Form horizontal form={this.props.form}>
        <Form.Item labelCol={{ span: 6 }} wrapperCol={{ span: 18 }} label={`${this.props.title} ID：`}>
          <Input {...idProps} required placeholder={`输入${this.props.title} ID`}/>
        </Form.Item>
      </Form>
    )
  }
}

UserRoleAddModalForm = Form.create({})(UserRoleAddModalForm);

class UserRoleAddModal extends React.Component {

  constructor(props) {
    super(props);
    this.state = { visible: false }
  }

  showModal(type, title, user) {
    const { form } = this.refs
    this.setState({
      visible: true,
      title: title,
      type: type,
      user: user
    });
    if (form)
      form.resetFields()
  }

  handleOk() {
    const {form} = this.refs;
    const me = this;
    form.validateFieldsAndScroll((errors, values) => {
      if (!!errors) {
        return;
      }

      const {type,user} = this.state

      const roles = {
        type: type == 'company' ? 'COMPANY_ADMIN' : 'DEV_TEAM_LEADER',
        userId: user.id,
        targetId: values.id
      }

      me.setState({
        confirmLoading: true
      });

      me.props.onSave(roles)
        .then(()=> {
          this.hideModel()
        });
    })
  }

  hideModel() {
    this.setState({
      visible: false,
      confirmLoading: false
    });
  }

  render() {
    return (
      <Modal title={this.state.title}
        visible={this.state.visible}
        onOk={::this.handleOk}
        confirmLoading={this.state.confirmLoading}
        onCancel={::this.hideModel}
        title={`新增${this.state.title}权限`}>
        <UserRoleAddModalForm title={this.state.title} ref="form"/>
      </Modal>
    );
  }
}

export default UserRoleAddModal;
